import axios from 'axios'
import store from '@/store'

const exportFile = function(api, params, fileName) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: process.env.VUE_APP_BASE_API + api, // 请求地址
      data: params,
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(
      response => {
        let blob = new Blob([response.data], { type: 'application/vnd.ms-excel' })
        let file_name = `${fileName ? fileName : Date.parse(new Date())}.xlsx`
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, file_name)
        } else {
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = file_name
          link.click()
          window.URL.revokeObjectURL(link.href)  //释放内存
        }
        resolve(response.data)
      },
      err => {
        reject(err)
      }
    )
  })
}
export default exportFile
